package com.neighbor.neighborhooduser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neighbor.neighborhooduser.controller.NotificationWebSocketHandler;
import com.neighbor.neighborhooduser.pojo.Notifications;
import com.neighbor.neighborhooduser.dao.INotificationsDao;
import com.neighbor.neighborhooduser.service.INotificationsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统通知表 服务实现类
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:13:04
 */
@Service
public class NotificationsServiceImpl extends ServiceImpl<INotificationsDao, Notifications> implements INotificationsService {
    @Autowired
    INotificationsDao notificationsDao;

    @Override
    public Notifications latestNotice() {
        return notificationsDao.latestNotice();
    }

    @Override
    public boolean sendNotification(Notifications notification) {
        notification.setIsRead((byte) 0);
        notification.setCreatedAt(new Date());
        boolean result = save(notification);

        if (result && notification.getUserId() != null) {
            // 发送WebSocket消息给指定用户
            JSONObject message = new JSONObject();
            message.put("type", "notification");
            message.put("data", notification);
            NotificationWebSocketHandler.sendMessageToUser(notification.getUserId(), message.toJSONString());
        }

        return result;
    }

    @Override
    public boolean sendBroadcastNotification(Notifications notification) {
        // 保存系统通知（userId为0表示全体用户）
        notification.setUserId(0);
        notification.setIsRead((byte) 0);
        notification.setCreatedAt(new Date());
        boolean result = save(notification);

        if (result) {
            // 广播WebSocket消息给所有在线用户
            JSONObject message = new JSONObject();
            message.put("type", "notification");
            message.put("data", notification);
            NotificationWebSocketHandler.broadcast(message.toJSONString());
        }

        return result;
    }

    @Override
    public IPage<Notifications> getUserNotifications(Integer userId, Integer type, int pageNum, int pageSize) {
        Page<Notifications> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectUserNotifications(page, userId, type);
    }

    @Override
    public boolean markAsRead(Integer notificationId, Integer userId) {
        Notifications notification = getById(notificationId);
        if (notification != null && (notification.getUserId().equals(userId) || notification.getUserId() == 0)) {
            notification.setIsRead((byte) 1);
//            notification.setReadAt(new Date());
            return updateById(notification);
        }
        return false;
    }

    @Override
    public boolean markAllAsRead(Integer userId) {
        // 实际项目中可以写一个批量更新的SQL
        return true;
    }

    @Override
    public Integer getUnreadCount(Integer userId) {
        return baseMapper.selectUnreadCount(userId);
    }

    @Override
    public List<Notifications> getAllNotification(Integer userId) {
        return notificationsDao.getAllNotification(userId);
    }
}
